package jzbank.dao.serializabledao.impl;

import jzbank.common.exception.SyncException;
import jzbank.dao.serializabledao.DataExportDao;
import jzbank.pojo.comfig.DatabasePO;
import jzbank.pojo.dto.DataExportDTO;
import jzbank.util.SerializationUtil;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class DataExportDaoImpl implements DataExportDao {
    private static final String DATA_FILE = "./dataexport/dataexport_data.ser"; // 数据文件的文件名

    // 添加数据导出信息
    @Override
    public void addDataExport(DataExportDTO dataExportDTO) {
        List<DataExportDTO> dataExportDTOList = getAllDataExport();
        dataExportDTOList.add(dataExportDTO);

        SerializationUtil.serializeObjectToFile(dataExportDTOList, DATA_FILE);

    }

    // 删除数据导出信息
    @Override
    public void deleteDataExport(String id) {
        List<DataExportDTO> dataExportDTOList = getAllDataExport();
        dataExportDTOList.removeIf(dataExportDTO -> dataExportDTO.getTid().equals(id));


        SerializationUtil.serializeObjectToFile(dataExportDTOList, DATA_FILE);

    }

    // 更新数据导出信息
    @Override
    public void updateDataExport(DataExportDTO updatedDataExportDTO) {
        List<DataExportDTO> dataExportDTOList = getAllDataExport();
        for (int i = 0; i < dataExportDTOList.size(); i++) {
            if (dataExportDTOList.get(i).getTid().equals(updatedDataExportDTO.getTid())) {
                dataExportDTOList.set(i, updatedDataExportDTO);
                break;
            }
        }


        SerializationUtil.serializeObjectToFile(dataExportDTOList, DATA_FILE);

    }

    // 获取所有数据导出信息
    @Override
    public List<DataExportDTO> getAllDataExport() {
        List<DataExportDTO> dataExportDTOList = new ArrayList<>();

        Object obj = SerializationUtil.deserializeObjectFromFile(DATA_FILE);
        if (obj instanceof List<?>) {
            dataExportDTOList = (List<DataExportDTO>) obj;
        }

        return dataExportDTOList;
    }

    // 根据数据导出id查找数据导出信息
    @Override
    public DataExportDTO getDataExportId(String id) {
        List<DataExportDTO> dataExportDTOList = getAllDataExport();
        DataExportDTO dataExportDTO = new DataExportDTO();
        for (int i = 0; i < dataExportDTOList.size(); i++) {
            if (dataExportDTOList.get(i).getTid().equals(id)) {
                dataExportDTO = dataExportDTOList.get(i);
                break;
            }
        }
        return dataExportDTO;
    }

    @Override
    public DataExportDTO getDataExportByName(String name) {
        List<DataExportDTO> dataExportDTOList = getAllDataExport();
        DataExportDTO dataExportDTO = new DataExportDTO();
        for (int i = 0; i < dataExportDTOList.size(); i++) {
            if (dataExportDTOList.get(i).getTaskCode().equals(name)) {
                dataExportDTO = dataExportDTOList.get(i);
                break;
            }
        }
        return dataExportDTO;
    }
}
